<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\admin\model\Banner as BannerModel;
use app\admin\model\Classify as ClassifyModel;
use app\admin\model\Buy as BuyModel;
use think\Env;
/**
 * 示例接口
 */
class Shop extends Api
{
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['index','list','detail','search'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = [];

    public function index()
    {
        //banner图
        $model = new BannerModel();
        $banner = $model
            ->where('type', 2)
            ->field('id,image')
            ->select();
        $banner = addWebSiteUrl($banner, ['image']);

        //分类
        $model = new ClassifyModel();
        $classify = $model
            ->where('status', 1)
            ->field('id,image,title')
            ->select();
        $classify = addWebSiteUrl($classify, ['image']);

        //推荐
        $model = new BuyModel();
        $buy = $model
            ->where('type', 1)
            ->field('id,image,title,price')
            ->limit(0,4)
            ->select();
        $buy = addWebSiteUrl($buy, ['image']);

        return sucessMsg(200, '获取成功！',
            [
                'banner' => $banner,
                'classify' => $classify,
                'buy' => $buy
            ]);
    }

    //列表
    public function list(){
        //$page=input('page',1);
        $id = input('classify_id');
        if (empty($id)) {
            return errMsg(402, '请传分类id！');
        }
        $model = new BuyModel();
        $list = $model
            ->where('classify_id', $id)
            ->field('id,image')
           // ->page($page,10)
            ->select();
        $list = addWebSiteUrl($list, ['image']);

        return sucessMsg(200, '获取成功！',
            [
                'list' => $list
            ]);
    }

    //商品详情
    public function detail()
    {
        $data = input('id');
        if (empty($data)) {
            return errMsg(422, '请输入id');
        }
        $model = new BuyModel();
        $cate = $model
            ->where('id', $data)
            ->field('id,image,price,disc,sell,title,desc,safe,address,content')
            ->find();

        if (empty($cate)) {
            return errMsg(422, '找不到数据');
        }

        $cate->image = Env::get('database.yuming') . $cate->image;

        return sucessMsg(200, '详情获取成功！', ['cate' => $cate]);
    }

    public function search(){
        $title=input('title');
        $where['title']= ['like',"%".$title."%"];
        //$where['id'] = ['>',10];
        $model = new BuyModel();
        $cate = $model
            //->where('id', $data)
            ->where($where)
            ->field('id,image,price,disc,sell,title,desc,safe,address,content')
            ->select();
        if (empty($title)) {
            return errMsg(422, '请输入关键词');
        }
        $cate = addWebSiteUrl($cate, ['image']);
        return sucessMsg(200, '详情获取成功！', ['cate' => $cate]);
    }

}
